#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

// #define int int64_t
#define endl "\n"

using PII = pair<int, int>;
using TII = tuple<int, int, int>;
template<class T> using V = vector<T>;
using i64 = long long;
using i128 = __int128;
using u64 = unsigned long long;

#define FOR(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define FOR2(i, a, b, c) for (int i = (int)(a); i <= (int)(b); i += c)
#define FORD(i, b, a) for (int i = (int)(a); i >= (int)(b); i--)
#define FORD2(i, b, a, c) for (int i = (int)(a); i >= (int)(b); i -= c)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define lowbit(x) ((x) & (-x))
#define RE return;
#define Yes cout << "Yes" << endl;
#define YES cout << "YES" << endl;
#define No cout << "No" << endl;
#define NO cout << "NO" << endl;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define sz(x) (int)(x).size()

void solve() {
    int n, m;
    cin >> n >> m;
    V<int> a(n + 1, 0), b(m + 1, 0);
    FOR(i, 1, n) cin >> a[i];
    FOR(i, 1, m) cin >> b[i];

    FOR(i, 1, n) {
        if (a[i] < m) {
            No;
            RE;
        }
    }
    FOR(i, 1, m) {
        if (b[i] < n) {
            No;
            RE;
        }
    }

    V<V<int>> ans(n + 1, V<int>(m + 1, 0));
    V<int> vis(n * m + 1, 0), s;
    map<int, V<array<int, 2>>> mp;
    multiset<int> c;

    FOR(i, 1, n) {
        FOR(j, 1, m) {
            if (a[i] == b[j]) {
                ans[i][j] = a[i];
                vis[a[i]] = 1;
            } else {
                int t = min(a[i], b[j]);
                mp[t].pb({i, j});
                c.insert(t);
            }
        }
    }

    FOR(i, 1, n * m) {
        if (!vis[i]) {
            s.eb(i);
        }
    }

    int tot = sz(s);
    FORD(i, 0, tot - 1) {
        int val = s[i];
        auto it = c.lower_bound(val);
        if (it == c.end()) {
            No;
            RE;
        }
        int t = *it;
        auto [x, y] = mp[t].back();
        ans[x][y] = val;
        mp[t].pop_back();
        c.erase(c.find(t));
    }

    FOR(i, 1, n) {
        int mx = 0;
        FOR(j, 1, m) {
            mx = max(mx, ans[i][j]);
        }
        if (mx != a[i]) {
            No;
            RE;
        }
    }

    FOR(j, 1, m) {
        int mx = 0;
        FOR(i, 1, n) {
            mx = max(mx, ans[i][j]);
        }
        if (mx != b[j]) {
            No;
            RE;
        }
    }

    Yes;

    FOR(i, 1, n) {
        FOR(j, 1, m) {
            cout << ans[i][j] << ' ';
        }
        cout << endl;
    }
}

signed main() {
    int Task = 1;

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> Task;
    while (Task--) {
        solve();
    }

    return 0;
}
